﻿using MicroBlog.Models;
using Microsoft.EntityFrameworkCore;

namespace MicroBlog.Data
{
    /// <summary>
    /// 数据库上下文对象，用于完成对数据库的操作
    /// </summary>
    public class MicroBlogContext : DbContext
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="options">选项</param>
        public MicroBlogContext(DbContextOptions<MicroBlogContext> options) : base(options) { }
        /// <summary>
        /// 配置模型的数据库属性
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<User>(etb =>
            {
                etb.HasKey(e => e.Id);
                etb.Property(e => e.Id).ValueGeneratedOnAdd();
                etb.Property(e => e.Name).IsRequired().HasMaxLength(64);
                etb.HasIndex(e => e.Name).IsUnique();
                etb.Property(e => e.Secret).HasMaxLength(1024);
                etb.ToTable("Users");
            });
            modelBuilder.Entity<Role>(etb =>
            {
                etb.HasKey(e => e.Id);
                etb.Property(e => e.Id).ValueGeneratedOnAdd();
                etb.Property(e => e.Name).IsRequired().HasMaxLength(50);
                etb.HasIndex(e => e.Name).IsUnique();
                etb.Property(e => e.Description).HasMaxLength(100);
                etb.ToTable("Roles");
            });

            modelBuilder.Entity<UserRole>(etb =>
            {
                etb.HasKey(e => new { e.UserId, e.RoleId });
                etb.ToTable("UserRoles");
            });

            modelBuilder.Entity<Permission>(etb =>
            {
                etb.HasKey(e => e.Id);
                etb.Property(e => e.Id).ValueGeneratedOnAdd();
                etb.Property(e => e.Module).IsRequired().HasMaxLength(50);
                etb.Property(e => e.Controller).IsRequired().HasMaxLength(50);
                etb.Property(e => e.Action).IsRequired().HasMaxLength(50);
                etb.ToTable("Permissions");
            });

            modelBuilder.Entity<RolePermission>(etb =>
            {
                etb.HasKey(e => new { e.RoleId, e.PermissionId });
                etb.ToTable("RolePermissions");
            });

            modelBuilder.Entity<Tag>(etb =>
            {
                etb.HasKey(e => e.Id);
                etb.Property(e => e.Id).ValueGeneratedOnAdd();
                etb.Property(e => e.Name).IsRequired().HasMaxLength(64);
                etb.ToTable("Tags");
            });
            modelBuilder.Entity<Blog>(etb =>
            {
                etb.HasKey(e => e.Id);
                etb.Property(e => e.Id).ValueGeneratedOnAdd();
                etb.Property(e => e.Title).IsRequired().HasMaxLength(100);
                etb.Property(e => e.Cover).HasMaxLength(100);
                etb.Property(e => e.Overview).IsRequired().HasMaxLength(400);
                etb.ToTable("Blogs");
            });
            modelBuilder.Entity<BlogContent>(etb =>
            {
                etb.HasKey(e => e.BlogId);
                etb.ToTable("BlogContents");
            });

        }
        #region 身份验证相关
        /// <summary>
        /// 系统用户
        /// </summary>
        public DbSet<User> Users { get; set; }
        /// <summary>
        /// 系统角色
        /// </summary>
        public DbSet<Role> Roles { get; set; }
        /// <summary>
        /// 用户与角色的关系
        /// </summary>
        public DbSet<UserRole> UserRoles { get; set; }
        /// <summary>
        /// 系统权限
        /// </summary>
        public DbSet<Permission> Permissions { get; set; }
        /// <summary>
        /// 角色与权限的关系
        /// </summary>
        public DbSet<RolePermission> RolePermissions { get; set; }
        #endregion

        #region 博客相关
        /// <summary>
        /// 标签
        /// </summary>
        public DbSet<Tag> Tags { get; set; }
        /// <summary>
        /// 博客
        /// </summary>
        public DbSet<Blog> Blogs { get; set; }
        /// <summary>
        /// 博客的内容
        /// </summary>
        public DbSet<BlogContent> BlogContents { get; set; }

        #endregion

    }
}
